function initChart(id, data, title) {

    $(id).dxPieChart({
        size: {
            width: 400
        },
        dataSource: data,
        palette: "Soft Pastel",
        legend: {
            horizontalAlignment: "center",
            verticalAlignment: "bottom",
            margin: 0
        },
        series: [
            {
                type: "doughnut",
                argumentField: "parametro",
                valueField: "area",
                label: {
                    visible: true,
                    font: {
                        size: 10
                    },
                    connector: {
                        visible: true,
                        width: 0.5
                    },
                    position: "columns",
                    customizeText: function(arg) {
                        return arg.valueText + " ( " + arg.percentText + ")";
                    }

                }
            }
        ],
        'title': title
    });
}

$(document).ready(function() {
    var count = 0;
    menuClass('dashboard');
    $.blockUI({
        message: "Aguarde un momento por favor"
    });

    $.ajax({
        url: '/admin/common/systemcapacity',
        type: 'post',
        dataType: 'json',
        success: function(data) {
            console.log(data);
            var cpu = [{'parametro': 'Usado', 'area': data.cpu.usado},
                {'parametro': 'Libre', 'area': data.cpu.libre}];
            initChart("#chartCPU", cpu, "CPU utilizado (MHz)");

            var memory = [{'parametro': 'Usado', 'area': data.memory.usado},
                {'parametro': 'Libre', 'area': data.memory.libre}];
            initChart("#chartMemory", memory, "Memoria utilizada (MB)");

            var storage = [{'parametro': 'Usado', 'area': data.storage.usado},
                {'parametro': 'Libre', 'area': data.storage.libre}];
            initChart("#chartStorage", storage, "Alamacenamiento utilizado (MB)");

            count++;
            if (count === 3)
                $.unblockUI();
        },
        error: function(data) {
            $("#div-alert").append('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Error!</strong> Ocurrio un error inesperado.</div>');
            $.unblockUI();
        }
    });

    $.ajax({
        url: '/admin/common/cantidadvm',
        type: 'post',
        dataType: 'json',
        success: function(data) {
//            bootbox.alert(data.cuenta);
            $('#total-vm').html('VMs en el sistema: ' + data.response);
//            $('#total-vm-encendidas').html('VMs encendidas en el sistema: ' + data.response);
            count++;
            if (count === 3)
                $.unblockUI();
        },
        error: function(a, b, c) {
            $.unblockUI();
        }
    });

    $.ajax({
        url: '/admin/common/cantidadlab',
        type: 'post',
        dataType: 'json',
        success: function(data) {
            $('#total-labs').html('Laboratorios en el sistema: ' + data.response);
            count++;
            if (count === 3)
                $.unblockUI();
        },
        error: function(a, b, c) {
            $.unblockUI();
        }
    });
});

